package pages;

import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import utils.Log4Test;

import javax.xml.soap.SOAPPart;
import java.util.List;

/**
 * Created by w on 07.07.14.
 */
public class CommonNotebookPage extends TestBase {
    protected By mainTab = By.className("m-main-i active");
    protected By mainTabsList = By.xpath(".//*[@class='m-main-t-i']/div[@class='m-main-i active']");

    protected String mainTabText = "Ноутбуки, планшеты и компьютеры";
    protected By manuafcturerList = By.xpath(".//li[@class='m-cat-l-i']/h4[contains( text(),'Производители')]/../ul/li/a");
    protected int manufacturecount = 8;

    public void verifyMainTabNameActive() {

        Log4Test.info("VerifyMainTabNameActive" + mainTabText);
        List<WebElement> mainTabs = webDriver.findElements(mainTabsList);
        Assert.assertEquals(mainTabs.size(), 1);
        Assert.assertEquals(mainTabs.get(0).findElement(By.xpath("..")).getAttribute("id"), "computers-notebooks");
    }

    public void verifyManufactNumber() {
        Log4Test.info("Verify Manufacter Number=" + manufacturecount);
        List<WebElement> allmanufactures = webDriver.findElements(manuafcturerList);

        int count = allmanufactures.size();
        System.out.println(count);
        Assert.assertEquals(count, manufacturecount);
    }

    public void verifyManufacturerApplePresent() {
        Log4Test.info("verify Manufacturer Apple Present");
        List<WebElement> allmanufactures = webDriver.findElements(manuafcturerList);
/*
        WebElement appleFound = null;
*/
        int count = 0;
        for (int i = 0; i < allmanufactures.size(); i++) {

            if (allmanufactures.get(i).getText().equals("Apple")) {
               WebElement appleFound = allmanufactures.get(i);
                count++;
            }
        }
       /* appleFound.click();*/
        Assert.assertEquals(count, 1);
    }

    public ApplePage clickApple() {
        Log4Test.info("To ApplePage switch");
        List<WebElement> allmanufactures = webDriver.findElements(manuafcturerList);
        WebElement appleFound = null;

        for (int i = 0; i < allmanufactures.size(); i++) {

            if (allmanufactures.get(i).getText().equals("Apple")) {
                appleFound = allmanufactures.get(i);

            }
        }
        appleFound.click();
        return new ApplePage();
    }

}



